package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ：lintao
 * @date ：Created in 2020/12/7
 * @description ：
 * @version: 1.0
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return roleService.findPage(queryPageBean);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] menuIds, Integer[] permissionIds) {
        try {
            roleService.add(role, menuIds, permissionIds);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Role role = roleService.findById(id);
            return new Result(true, MessageConstant.GET_ROLE_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ROLE_FAIL);
        }
    }

    @RequestMapping("/findRoleAndMenu")
    public Result findRoleAndMenu(Integer roleId) {
        try {
            List<Integer> list = roleService.findRoleAndMenu(roleId);
            return new Result(true, MessageConstant.GET_ROLE_AND_MENU_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ROLE_AND_MENU_FAIL);
        }
    }

    @RequestMapping("/findRoleAndPermission")
    public Result findRoleAndPermission(Integer roleId) {
        try {
            List<Integer> list = roleService.findRoleAndPermission(roleId);
            return new Result(true, MessageConstant.GET_ROLE_AND_PERMISSION_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ROLE_AND_PERMISSION_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] menuIds, Integer[] permissionIds) {
        try {
            roleService.edit(role, menuIds, permissionIds);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    @RequestMapping("/deleteById")
    public Result deleteById(Integer id) {
        try {
            roleService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (RuntimeException re) {
            return new Result(false, re.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> roleList = roleService.findAll();
            return new Result(true,MessageConstant.GET_ROLE_LIST_SUCCESS,roleList);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ROLE_LIST_FAIL);
        }
    }
}
